﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 原料发货记录管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected string sql =
@"select t1.ID
,t1.原料ID
,t1.供货ID
,case when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
else '未知分厂'
end as 分厂
,t1.库区
,t1.车号
,t1.日期
,t1.重量
,isnull(t1.标重,0) as 标重
,t1.支数
,t1.明细
,dbo.原料品名(t1.原料ID) as 原料
,t2.名称 as 供货单位
,t3.单据号 as 出库单
from 原料发货记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID
left outer join 原料入出库 t3 on t3.ID=t1.出库单ID
";
        private string mxlist="";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //权限管理子程序==========================================================
                string opp3 = (string)Session["OPP3"];
                if (opp3 == null || opp3 == ",")
                {
                    this.LeiBie.DataBind();
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    this.DS_LeiBie.SelectCommand = "select ID, [品名]+' '+[钢级]+' '+[规格] AS 名称 FROM 原料 where [ID] in (" + opp3 + ")";
                }
                this.LeiBie.DataBind();
                //权限管理子程序==========================================================
                this.GongHuo.DataBind();
                this.Button_Query_Click(null, null);

            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }


            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.Button_MingXi.Enabled = false;
            this.Button_SelMX.Enabled = false;
            this.Button_Out.Enabled = false;

            this.GridView1.SelectedIndex = -1;

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// SQL字符串的拼接工作
        /// </summary>
        /// <returns></returns>
        protected string GetState()
        {
            string state = "";

            //给原料类别一个默认值1
            if (this.LeiBie.Text != "0")
            {
                state += " and t1.原料ID=" + this.LeiBie.Text;
            }

            if (this.GongHuo.Text != "0")
            {
                state += " and t1.供货ID=" + this.GongHuo.Text;
            }

            if (this.FenChang.Text != "0")
            {
                state += " and t1.分厂=" + this.FenChang.Text;
            }

            //单独处理一下合格未入库情况
            if (this.XianShi.Text != "")
            {
                if (this.XianShi.Text != "21")
                {
                    //去除合格未入库情况一切照旧
                    //state += " and t1.状态=" + this.XianShi.Text;
                }
                else
                {
                    state += "and t3.单据号 IS NULL ";
                }
            }
            int kq;
            if(int.TryParse(this.KuQu.Text,out kq))
            {
                state += " and t1.库区=" + kq;
            }
            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {

            this.LeiBie.Text = "0";
            this.GongHuo.Text = "0";
            this.FenChang.Text = "0";
            this.KuQu.Text = "";

            this.XianShi.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.GetSelectedList();
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        private string GetSelectedList()
        {
            string s="";
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                if (((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked)
                {

                    s += ","+this.GridView1.DataKeys[i].Values[0].ToString();
                    mxlist = this.GridView1.DataKeys[i].Values[2].ToString();
                }
            }
            if (s.Length > 0)
            {
                s = s.Substring(1);
            }
            return s;
        }
        

        protected void Button_Out_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料出库单录入.aspx?list=" + list + "',770,440);</script>");
            }
        }

        protected void Check_All_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                ((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked = s.Checked;
                this.GridView1.Rows[i].CssClass = cs;
            }
            this.Set_Button_State();
        }
        protected void Check_Item_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }

            ((GridViewRow)s.Parent.Parent).CssClass = cs;
            this.Set_Button_State();
        }
        private void Set_Button_State()
        {
            string list = GetSelectedList();
            if (list != "" )
            {
                if(list.IndexOf(',') < 0)
                {
                    
                    this.Button_SelMX.Enabled = true;
                    this.Button_Delete.Enabled = true;
                    this.Button_Edit.Enabled = true;
                    if (mxlist == "")
                    {
                        this.Button_MingXi.Enabled = false;
                    }
                    else
                    {
                        this.Button_MingXi.OnClientClick = "parent.swin2_load('/Store/原料发货明细.aspx?list=" + mxlist + "',1100,800,'yes');";
                        this.Button_MingXi.Enabled = true;
                    }

                    this.Button_SelMX.OnClientClick = "parent.swin_load('/Store/原料到发明细.aspx?type=fh&id=" + list + "',600,300);";
                    this.Button_Edit.OnClientClick = "parent.swin2_load('/Store/原料发货录入.aspx?id=" + list + "',600,300);";
                }
                else
                {
                    this.Button_MingXi.Enabled = false;
                    this.Button_SelMX.Enabled = false;
                    this.Button_Delete.Enabled = false;
                    this.Button_Edit.Enabled = false;
                }
                this.Button_Out.Enabled = true;
                
            }
            else
            {
                this.Button_Delete.Enabled = false;
                this.Button_Edit.Enabled = false;
                this.Button_Out.Enabled = false;
                this.Button_MingXi.Enabled = false;
                this.Button_SelMX.Enabled = false;
                this.Button_Edit.Enabled = false;
            }

        }
        
    }
}
